/*
 * Copyright 2009 XueSong Guo.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package cn.webwheel.tutorials.helloworld;

import cn.webwheel.Action;
import cn.webwheel.ResultInterpreter;
import cn.webwheel.WebContext;

import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

// String结果类型的解释器
public class StringResultInterpreter implements ResultInterpreter<String> {
    public void init() throws Exception {
    }

    public boolean render(String result, WebContext ctx, Action action) throws Exception {
        HttpServletResponse response = ctx.getResponse();
        // 设置http响应的相关参数
        response.setContentType("text/plain");
        response.setCharacterEncoding("utf-8");
        PrintWriter writer = response.getWriter();
        // 向客户端直接输出字符串结果
        writer.println(result);
        // 从request中获得访问次数，CountFilter所设置其值
        writer.println("访问次数：" + ctx.getRequest().getAttribute("count"));
        // 表示已处理了此次web请求
        return true;
    }

    public void destroy() {
    }
}
